package cn.stylefeng.guns.modular.controller;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.config.redislock.CacheLock;
import cn.stylefeng.guns.config.redislock.CacheParam;
import cn.stylefeng.guns.modular.model.params.WorkBookParam;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

@Controller
public class TestRedisController {

    Logger logger = LoggerFactory.getLogger(TestRedisController.class);

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @GetMapping("/test001")
    public void test001(){
        String aa =  redisTemplate.opsForValue().get("as_value");
        if(StringUtils.isEmpty(aa)){
            redisTemplate.opsForValue().set("as_value","123");
        }
    }

    @GetMapping("/test002")
    @CacheLock(prefix="redisTest_test002",expire=20)
    @ResponseBody
    public ResponseData test002(@CacheParam @RequestParam String redisParamOne,@CacheParam @RequestParam String redisParamTwo){
        logger.info("test002 param is {},{}",redisParamOne,redisParamTwo);
        logger.info(DateUtil.format(new Date(),"yyyyMMdd"));
        System.out.println(DateUtil.parse("20201009","yyyyMMdd"));
        return ResponseData.success();
    }

    @PostMapping("/test003")
    @ResponseBody
    @CacheLock(prefix = "redisTest_test003",expire = 20)
    public ResponseData test003(@Valid @RequestBody WorkBookParam workBookParam){
        logger.info("test002 param is {}",workBookParam);
        return ResponseData.success();
    }
}
